
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        
        <title>FibJS - JavaScript on Fiber</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <link href="css/flexslider.min.css" rel="stylesheet" type="text/css" media="all"/>
        <link href="css/line-icons.min.css" rel="stylesheet" type="text/css" media="all"/>
        <link href="css/elegant-icons.min.css" rel="stylesheet" type="text/css" media="all"/>
        <link href="css/lightbox.min.css" rel="stylesheet" type="text/css" media="all"/>
        <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" media="all"/>
        <link href="css/theme-aquatica.css" rel="stylesheet" type="text/css" media="all"/>
        <link href="css/custom.css" rel="stylesheet" type="text/css" media="all"/>
        <link rel="icon" type="image/png" href="img/icon.png">
        <!--[if gte IE 9]>

        <!--[if gte IE 9]>
        	<link rel="stylesheet" type="text/css" href="css/ie9.css" />
		<![endif]-->
        <script src="js/modernizr-2.6.2-respond-1.1.0.min.js"></script>
    </head>
    <body>
    	<div class="loader">
    		<div class="spinner">
			  <div class="double-bounce1"></div>
			  <div class="double-bounce2"></div>
			</div>
    	</div>
				
		<div class="nav-container">
			<nav class="top-bar">
				<div class="container">
				
					<div class="row nav-menu">
						<div class="col-sm-3 col-md-2 columns">
							<a href="index.html">
								<img class="logo logo-light" alt="Logo" src="img/logo-light.png">
								<img class="logo logo-dark" alt="Logo" src="img/logo-dark.png">
							</a>
						</div>
					
						<div class="col-sm-9 col-md-10 columns  text-right">
							<ul class="menu">
								<li><a href="index.html">Home</a>
								</li>
								<li><a href="guide.html">Guide</a>
								</li>
								<li><a  href="moudle.html">Moudle</a>
								</li>
								<li><a href="object.html">Object</a>
								</li>
								<li><a href="download.html">Download</a>
								</li>
							</ul>
						</div>
					</div><!--end of row-->
					
					<div class="mobile-toggle">
						<i class="icon icon_menu"></i>
					</div>
					
				</div><!--end of container-->
			</nav>
		</div>
		<section class="main-container primary-features">
			<div class="container search">
                <div class="row">
                    <div class="col-sm-12 col-md-9 columns">
                       <h1>Guide 开发指南</h1>
                    </div>
                    <div class="col-sm-12 col-md-3 columns text-right">
                        <input type="text" class="form-control search" placeholder="Search for...">
                    </div>
                </div>
            </div>
        </section>
        <section class="main-container">
			<div class="container">
                <div class="row">
                    <div class="col-xs-12">
                        <a href=#>首页</a> / <a href=#>开发指南</a> / <a href=#>开发指南</a>
                    </div>
                </div>
            </div>
        </section>

        <section class="main-container">
            <div class="container">
                <div class="row">
                    <div class="col-md-3">
                        <div class="panel-group" id="accordion" role="tablist">  <div class="panel"><div class="panel-heading" role="tab" id="headingOne"><h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#collapse_guide" aria-controls="collapse_guide"> 开发指南 </a></h4></div><div id="collapse_guide" class="panel-collapse collapse in" aria-labelledby="headingOne"><div class="panel-body"> <ul>
                                <li><a href="about.md.html">fibjs 是什么？</a></li>
                                <li><a href="install.md.html">安装运行环境</a></li>
                                <li><a href="hello.md.html">hello, world</a></li>
                                <li><a href="test.md.html">美好生活从测试开始</a></li>
                                </ul>
                                 </div></div></div>  <div class="panel"><div class="panel-heading" role="tab" id="headingOne"><h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#collapse_module" aria-controls="collapse_module"> 基础模块 </a></h4></div><div id="collapse_module" class="panel-collapse collapse " aria-labelledby="headingOne"><div class="panel-body"> <ul>
                                <li><a href="../manual/module/ifs/assert.md.html">assert</a></li>
                                <li><a href="../manual/module/ifs/base32.md.html">base32</a></li>
                                <li><a href="../manual/module/ifs/base64.md.html">base64</a></li>
                                <li><a href="../manual/module/ifs/base64vlq.md.html">base64vlq</a></li>
                                <li><a href="../manual/module/ifs/bson.md.html">bson</a></li>
                                <li><a href="../manual/module/ifs/collection.md.html">collection</a></li>
                                <li><a href="../manual/module/ifs/console.md.html">console</a></li>
                                <li><a href="../manual/module/ifs/coroutine.md.html">coroutine</a></li>
                                <li><a href="../manual/module/ifs/crypto.md.html">crypto</a></li>
                                <li><a href="../manual/module/ifs/db.md.html">db</a></li>
                                <li><a href="../manual/module/ifs/encoding.md.html">encoding</a></li>
                                <li><a href="../manual/module/ifs/fs.md.html">fs</a></li>
                                <li><a href="../manual/module/ifs/gd.md.html">gd</a></li>
                                <li><a href="../manual/module/ifs/global.md.html">global</a></li>
                                <li><a href="../manual/module/ifs/gui.md.html">gui</a></li>
                                <li><a href="../manual/module/ifs/hash.md.html">hash</a></li>
                                <li><a href="../manual/module/ifs/hex.md.html">hex</a></li>
                                <li><a href="../manual/module/ifs/http.md.html">http</a></li>
                                <li><a href="../manual/module/ifs/iconv.md.html">iconv</a></li>
                                <li><a href="../manual/module/ifs/io.md.html">io</a></li>
                                <li><a href="../manual/module/ifs/json.md.html">json</a></li>
                                <li><a href="../manual/module/ifs/mq.md.html">mq</a></li>
                                <li><a href="../manual/module/ifs/net.md.html">net</a></li>
                                <li><a href="../manual/module/ifs/os.md.html">os</a></li>
                                <li><a href="../manual/module/ifs/path.md.html">path</a></li>
                                <li><a href="../manual/module/ifs/path_posix.md.html">path_posix</a></li>
                                <li><a href="../manual/module/ifs/path_win32.md.html">path_win32</a></li>
                                <li><a href="../manual/module/ifs/process.md.html">process</a></li>
                                <li><a href="../manual/module/ifs/profiler.md.html">profiler</a></li>
                                <li><a href="../manual/module/ifs/punycode.md.html">punycode</a></li>
                                <li><a href="../manual/module/ifs/querystring.md.html">querystring</a></li>
                                <li><a href="../manual/module/ifs/registry.md.html">registry</a></li>
                                <li><a href="../manual/module/ifs/ssl.md.html">ssl</a></li>
                                <li><a href="../manual/module/ifs/test.md.html">test</a></li>
                                <li><a href="../manual/module/ifs/timers.md.html">timers</a></li>
                                <li><a href="../manual/module/ifs/tty.md.html">tty</a></li>
                                <li><a href="../manual/module/ifs/url.md.html">url</a></li>
                                <li><a href="../manual/module/ifs/util.md.html">util</a></li>
                                <li><a href="../manual/module/ifs/uuid.md.html">uuid</a></li>
                                <li><a href="../manual/module/ifs/vm.md.html">vm</a></li>
                                <li><a href="../manual/module/ifs/ws.md.html">ws</a></li>
                                <li><a href="../manual/module/ifs/xml.md.html">xml</a></li>
                                <li><a href="../manual/module/ifs/zip.md.html">zip</a></li>
                                <li><a href="../manual/module/ifs/zlib.md.html">zlib</a></li>
                                <li><a href="../manual/module/ifs/zmq.md.html">zmq</a></li>
                                </ul>
                                 </div></div></div>  <div class="panel"><div class="panel-heading" role="tab" id="headingOne"><h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#collapse_object" aria-controls="collapse_object"> 内置对象 </a></h4></div><div id="collapse_object" class="panel-collapse collapse " aria-labelledby="headingOne"><div class="panel-body"> <ul>
                                <li><a href="../manual/object/ifs/asyncwait.md.html">AsyncWait</a></li>
                                <li><a href="../manual/object/ifs/blockqueue.md.html">BlockQueue</a></li>
                                <li><a href="../manual/object/ifs/buffer.md.html">Buffer</a></li>
                                <li><a href="../manual/object/ifs/bufferedstream.md.html">BufferedStream</a></li>
                                <li><a href="../manual/object/ifs/chain.md.html">Chain</a></li>
                                <li><a href="../manual/object/ifs/cipher.md.html">Cipher</a></li>
                                <li><a href="../manual/object/ifs/condition.md.html">Condition</a></li>
                                <li><a href="../manual/object/ifs/dbconnection.md.html">DbConnection</a></li>
                                <li><a href="../manual/object/ifs/digest.md.html">Digest</a></li>
                                <li><a href="../manual/object/ifs/event.md.html">Event</a></li>
                                <li><a href="../manual/object/ifs/eventemitter.md.html">EventEmitter</a></li>
                                <li><a href="../manual/object/ifs/eventinfo.md.html">EventInfo</a></li>
                                <li><a href="../manual/object/ifs/fiber.md.html">Fiber</a></li>
                                <li><a href="../manual/object/ifs/file.md.html">File</a></li>
                                <li><a href="../manual/object/ifs/gridfs.md.html">GridFS</a></li>
                                <li><a href="../manual/object/ifs/handler.md.html">Handler</a></li>
                                <li><a href="../manual/object/ifs/handlerex.md.html">HandlerEx</a></li>
                                <li><a href="../manual/object/ifs/heapgraphedge.md.html">HeapGraphEdge</a></li>
                                <li><a href="../manual/object/ifs/heapgraphnode.md.html">HeapGraphNode</a></li>
                                <li><a href="../manual/object/ifs/heapsnapshot.md.html">HeapSnapshot</a></li>
                                <li><a href="../manual/object/ifs/httpclient.md.html">HttpClient</a></li>
                                <li><a href="../manual/object/ifs/httpcollection.md.html">HttpCollection</a></li>
                                <li><a href="../manual/object/ifs/httpcookie.md.html">HttpCookie</a></li>
                                <li><a href="../manual/object/ifs/httphandler.md.html">HttpHandler</a></li>
                                <li><a href="../manual/object/ifs/httpmessage.md.html">HttpMessage</a></li>
                                <li><a href="../manual/object/ifs/httprequest.md.html">HttpRequest</a></li>
                                <li><a href="../manual/object/ifs/httpresponse.md.html">HttpResponse</a></li>
                                <li><a href="../manual/object/ifs/httpserver.md.html">HttpServer</a></li>
                                <li><a href="../manual/object/ifs/httpsserver.md.html">HttpsServer</a></li>
                                <li><a href="../manual/object/ifs/httpuploaddata.md.html">HttpUploadData</a></li>
                                <li><a href="../manual/object/ifs/image.md.html">Image</a></li>
                                <li><a href="../manual/object/ifs/int64.md.html">Int64</a></li>
                                <li><a href="../manual/object/ifs/leveldb.md.html">LevelDB</a></li>
                                <li><a href="../manual/object/ifs/list.md.html">List</a></li>
                                <li><a href="../manual/object/ifs/lock.md.html">Lock</a></li>
                                <li><a href="../manual/object/ifs/lrucache.md.html">LruCache</a></li>
                                <li><a href="../manual/object/ifs/map.md.html">Map</a></li>
                                <li><a href="../manual/object/ifs/memorystream.md.html">MemoryStream</a></li>
                                <li><a href="../manual/object/ifs/message.md.html">Message</a></li>
                                <li><a href="../manual/object/ifs/mongocollection.md.html">MongoCollection</a></li>
                                <li><a href="../manual/object/ifs/mongocursor.md.html">MongoCursor</a></li>
                                <li><a href="../manual/object/ifs/mongodb.md.html">MongoDB</a></li>
                                <li><a href="../manual/object/ifs/mongoid.md.html">MongoID</a></li>
                                <li><a href="../manual/object/ifs/mssql.md.html">MSSQL</a></li>
                                <li><a href="../manual/object/ifs/mysql.md.html">MySQL</a></li>
                                <li><a href="../manual/object/ifs/object.md.html">object</a></li>
                                <li><a href="../manual/object/ifs/pkey.md.html">PKey</a></li>
                                <li><a href="../manual/object/ifs/queue.md.html">Queue</a></li>
                                <li><a href="../manual/object/ifs/redis.md.html">Redis</a></li>
                                <li><a href="../manual/object/ifs/redishash.md.html">RedisHash</a></li>
                                <li><a href="../manual/object/ifs/redislist.md.html">RedisList</a></li>
                                <li><a href="../manual/object/ifs/redisset.md.html">RedisSet</a></li>
                                <li><a href="../manual/object/ifs/redissortedset.md.html">RedisSortedSet</a></li>
                                <li><a href="../manual/object/ifs/routing.md.html">Routing</a></li>
                                <li><a href="../manual/object/ifs/sandbox.md.html">SandBox</a></li>
                                <li><a href="../manual/object/ifs/seekablestream.md.html">SeekableStream</a></li>
                                <li><a href="../manual/object/ifs/semaphore.md.html">Semaphore</a></li>
                                <li><a href="../manual/object/ifs/service.md.html">Service</a></li>
                                <li><a href="../manual/object/ifs/smtp.md.html">Smtp</a></li>
                                <li><a href="../manual/object/ifs/socket.md.html">Socket</a></li>
                                <li><a href="../manual/object/ifs/sqlite.md.html">SQLite</a></li>
                                <li><a href="../manual/object/ifs/sslhandler.md.html">SslHandler</a></li>
                                <li><a href="../manual/object/ifs/sslserver.md.html">SslServer</a></li>
                                <li><a href="../manual/object/ifs/sslsocket.md.html">SslSocket</a></li>
                                <li><a href="../manual/object/ifs/stat.md.html">Stat</a></li>
                                <li><a href="../manual/object/ifs/stats.md.html">Stats</a></li>
                                <li><a href="../manual/object/ifs/stream.md.html">Stream</a></li>
                                <li><a href="../manual/object/ifs/subprocess.md.html">SubProcess</a></li>
                                <li><a href="../manual/object/ifs/tcpserver.md.html">TcpServer</a></li>
                                <li><a href="../manual/object/ifs/textcolor.md.html">TextColor</a></li>
                                <li><a href="../manual/object/ifs/timer.md.html">Timer</a></li>
                                <li><a href="../manual/object/ifs/urlobject.md.html">UrlObject</a></li>
                                <li><a href="../manual/object/ifs/websocket.md.html">WebSocket</a></li>
                                <li><a href="../manual/object/ifs/websockethandler.md.html">WebSocketHandler</a></li>
                                <li><a href="../manual/object/ifs/websocketmessage.md.html">WebSocketMessage</a></li>
                                <li><a href="../manual/object/ifs/webview.md.html">WebView</a></li>
                                <li><a href="../manual/object/ifs/worker.md.html">Worker</a></li>
                                <li><a href="../manual/object/ifs/x509cert.md.html">X509Cert</a></li>
                                <li><a href="../manual/object/ifs/x509crl.md.html">X509Crl</a></li>
                                <li><a href="../manual/object/ifs/x509req.md.html">X509Req</a></li>
                                <li><a href="../manual/object/ifs/xmlattr.md.html">XmlAttr</a></li>
                                <li><a href="../manual/object/ifs/xmlcdatasection.md.html">XmlCDATASection</a></li>
                                <li><a href="../manual/object/ifs/xmlcharacterdata.md.html">XmlCharacterData</a></li>
                                <li><a href="../manual/object/ifs/xmlcomment.md.html">XmlComment</a></li>
                                <li><a href="../manual/object/ifs/xmldocument.md.html">XmlDocument</a></li>
                                <li><a href="../manual/object/ifs/xmldocumenttype.md.html">XmlDocumentType</a></li>
                                <li><a href="../manual/object/ifs/xmlelement.md.html">XmlElement</a></li>
                                <li><a href="../manual/object/ifs/xmlnamednodemap.md.html">XmlNamedNodeMap</a></li>
                                <li><a href="../manual/object/ifs/xmlnode.md.html">XmlNode</a></li>
                                <li><a href="../manual/object/ifs/xmlnodelist.md.html">XmlNodeList</a></li>
                                <li><a href="../manual/object/ifs/xmlprocessinginstruction.md.html">XmlProcessingInstruction</a></li>
                                <li><a href="../manual/object/ifs/xmltext.md.html">XmlText</a></li>
                                <li><a href="../manual/object/ifs/zipfile.md.html">ZipFile</a></li>
                                <li><a href="../manual/object/ifs/zipinfo.md.html">ZipInfo</a></li>
                                <li><a href="../manual/object/ifs/zmqsocket.md.html">ZmqSocket</a></li>
                                </ul>
                                 </div></div></div>  </div>
                    </div>
                    <!--end of left menu-->
                    <div class="col-md-7 main-content">
                        <div class="row">
                           <div class="col-md-10 col-ms-12">
                                <h1 id="fibjs-是什么？">fibjs 是什么？</h1>
                            </div>
                            <div class="col-md-2 col-ms-12">
                                <div class="social-share share-component" data-sites="weibo,twitter" data-description="分享">
                                    <a class="social-share-icon icon-weibo" href="http://service.weibo.com/share/share.php?url=http%3A%2F%2Ffibjs.org%2Fdocs%2Fguide%2Fabout.md.html&amp;title=fibjs%20%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F%20-%20JavaScript%20on%20Fiber&amp;pic=http%3A%2F%2Ffibjs.org%2Fimgs%2Flogo.png&amp;appkey=" target="_blank"></a>
                                    <a class="social-share-icon icon-twitter" href="https://twitter.com/intent/tweet?text=fibjs%20%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F%20-%20JavaScript%20on%20Fiber&amp;url=http%3A%2F%2Ffibjs.org%2Fdocs%2Fguide%2Fabout.md.html&amp;via=http%3A%2F%2Ffibjs.org" target="_blank"></a>
                                </div>
                            </div>
                        </div>
                        
                        <p>fibjs 是一个主要为 web 后端开发而设计的应用服务器开发框架，它建立在 Google v8 JavaScript 引擎基础上，并且选择了和传统的 callback 不同的并发解决方案。fibjs 利用 fiber 在框架层隔离了异步调用带来的业务复杂性，极大降低了开发难度，并减少因为用户空间频繁异步处理带来的性能问题。</p>
                        <p>由于历史原因，JavaScript 主要被用于浏览器的 UI 处理，UI 开发是典型的单线程事件驱动模式，因此 JavaScript 也形成了以异步处理为主要编程范式。</p>
                        <p>随着 JavaScript 的成功，越来越多的人开始将 JavaScript 应用到其它的场景。与此同时，人们也越来越发现在很多场景下异步处理并不是最合适的选择。</p>
                        <div name="返璞归真，敏捷开发" data-unique="返璞归真，敏捷开发"></div><h3 id="返璞归真，敏捷开发">返璞归真，敏捷开发</h3>
                        <p>fibjs 在框架层使用 fiber 隔离了异步调用带来的业务复杂性，将 io 的异步处理封装为更加直观的同步调用，工程师只需要按照通常的同步业务逻辑编写代码，即可享有异步处理带来的巨大便利。</p>
                        <p>以下这段代码摘自 mysql 模块的文档：</p>
                        <pre><code class="lang-JavaScript"><div class="line-numbers"></div>conn.beginTransaction(<span class="hljs-function"><span class="hljs-params">err</span> =&gt;</span> {
                            <span class="hljs-keyword">if</span> (err) {
                                <span class="hljs-keyword">throw</span> err;
                            }
                            conn.query(<span class="hljs-string">'INSERT INTO posts SET title=?'</span>, title,
                                (error, results, fields) =&gt; {
                                    <span class="hljs-keyword">if</span> (error) {
                                        <span class="hljs-keyword">return</span> conn.rollback(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
                                            <span class="hljs-keyword">throw</span> error;
                                        });
                                    }

                                    <span class="hljs-keyword">var</span> log = <span class="hljs-string">'Post '</span> + results.insertId + <span class="hljs-string">' added'</span>;

                                    conn.query(<span class="hljs-string">'INSERT INTO log SET data=?'</span>, log,
                                        (error, results, fields) =&gt; {
                                            <span class="hljs-keyword">if</span> (error) {
                                                <span class="hljs-keyword">return</span> conn.rollback(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
                                                    <span class="hljs-keyword">throw</span> error;
                                                });
                                            }
                                            conn.commit(<span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> {
                                                <span class="hljs-keyword">if</span> (err) {
                                                    <span class="hljs-keyword">return</span> conn.rollback(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
                                                        <span class="hljs-keyword">throw</span> err;
                                                    });
                                                }
                                                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'success!'</span>);
                                            });
                                        });
                                });
                        });
                        </code></pre>
                        <p>在 fibjs 中，完成同样的工作，代码如下：</p>
                        <pre><code class="lang-JavaScript"><div class="line-numbers"></div>conn.trans(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
                            <span class="hljs-keyword">var</span> result = conn.execute(<span class="hljs-string">'INSERT INTO posts SET title=?'</span>, title);
                            <span class="hljs-keyword">var</span> log = <span class="hljs-string">'Post '</span> + results.insertId + <span class="hljs-string">' added'</span>;
                            conn.execute(<span class="hljs-string">'INSERT INTO log SET data=?'</span>, log);
                        });
                        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'success!'</span>);
                        </code></pre>
                        <p>如果你追求简洁，你甚至可以把代码写成这样：</p>
                        <pre><code class="lang-JavaScript"><div class="line-numbers"></div>conn.trans(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> conn.execute(<span class="hljs-string">'INSERT INTO log SET data=?'</span>,
                                <span class="hljs-string">'Post '</span> + conn.execute(<span class="hljs-string">'INSERT INTO posts SET title=?'</span>, title).insertId +
                                <span class="hljs-string">' added'</span>));
                        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'success!'</span>);
                        </code></pre>
                        <p>我们可以明显比较出两种不同的编程风格带来的差异。更少的代码会带来更少错误，随着代码的减少，代码的逻辑也更加清晰，无论是开发还是维护，都会从中获益。</p>
                        <div name="拥抱高能" data-unique="拥抱高能"></div><h3 id="拥抱高能">拥抱高能</h3>
                        <p>尽管我们可以很方便地通过扩充服务器来提高响应速度，但是性能仍然应该是选择一个开发框架的重要依据之一。随着 ES7 的推出，async 作为一种新的异步开发模式被引入 JavaScript。然而当我们享受 async 带来的同步风格时，也不得不面对它对性能的影响。</p>
                        <p>我们可以使用一段测试代码来比较不同的编程风格带来的性能差异：</p>
                        <pre><code class="lang-JavaScript"><div class="line-numbers"></div><span class="hljs-keyword">var</span> count = <span class="hljs-number">1000</span>;

                        <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test_async</span>(<span class="hljs-params">n</span>) </span>{
                            <span class="hljs-keyword">if</span> (n == count)
                                <span class="hljs-keyword">return</span>;
                            <span class="hljs-keyword">await</span> test_async(n + <span class="hljs-number">1</span>);
                        }

                        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test_callback</span>(<span class="hljs-params">n, cb</span>) </span>{
                            <span class="hljs-keyword">if</span> (n == count)
                                <span class="hljs-keyword">return</span> cb();

                            test_callback(n + <span class="hljs-number">1</span>, () =&gt; {
                                cb();
                            });
                        }

                        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test_sync</span>(<span class="hljs-params">n</span>) </span>{
                            <span class="hljs-keyword">if</span> (n == count)
                                <span class="hljs-keyword">return</span>;
                            test_sync(n + <span class="hljs-number">1</span>);
                        }

                        <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test</span>(<span class="hljs-params"></span>) </span>{
                            <span class="hljs-built_in">console</span>.time(<span class="hljs-string">"async"</span>);
                            <span class="hljs-keyword">await</span> test_async(<span class="hljs-number">0</span>);
                            <span class="hljs-built_in">console</span>.timeEnd(<span class="hljs-string">"async"</span>);

                            <span class="hljs-built_in">console</span>.time(<span class="hljs-string">"callback"</span>);
                            test_callback(<span class="hljs-number">0</span>, () =&gt; {
                                <span class="hljs-built_in">console</span>.timeEnd(<span class="hljs-string">"callback"</span>);
                            });

                            <span class="hljs-built_in">console</span>.time(<span class="hljs-string">"sync"</span>);
                            <span class="hljs-keyword">await</span> test_sync(<span class="hljs-number">0</span>);
                            <span class="hljs-built_in">console</span>.timeEnd(<span class="hljs-string">"sync"</span>);
                        }

                        test();
                        </code></pre>
                        <p>在最新的 v8 引擎下，这段代码的运行结果如下：</p>
                        <pre><code class="lang-sh"><div class="line-numbers"></div>async: 14.283ms
                        callback: 1.03ms
                        sync: 0.106ms
                        </code></pre>
                        <p>我们从测试结果可以明显知道，当项目中广泛应用 async 之后，服务器将花费大量时间用来处理 async 函数的调用和返回。我们在一些服务端应用的实际测试中也发现了这一点。而且这种性能的急剧下降，是完全不能接受的。</p>
                        <p>fibjs 由于采用 fiber，可以充分利用了 JavaScript 语言本身的特性，并且最大限度地发挥 v8 的优越性能。工程师可以很轻易地将服务器的性能发挥到极致。</p>
                        <div name="灵活选择范式而不被绑架" data-unique="灵活选择范式而不被绑架"></div><h3 id="灵活选择范式而不被绑架">灵活选择范式而不被绑架</h3>
                        <p>选择使用 fibjs 并不意味着你必须使用同步的开发风格，实际上 fibjs 支持你所见过的任何一种异步编程范式，并且可以灵活地在同步风格和异步风格之间切换。</p>
                        <p>无论是 callback 还是 async，都有一个致命的缺陷，那就是传染性。只要一个函数是 callback 或者 async，那么所有依赖它的其它函数都必须是 callback 或者 async。这在大规模软件开发中将带来巨大的开发成本。</p>
                        <p>以一个简单的服务器开发场景为例。在项目初期，我们选择了内存作为 session 数据存储，此时，我们可以使用 sync 方式直接读取和存储数据，并基于此开发出完整业务。随着业务规模的发展，我们需要把 session 数据存储到 redis 或者 mongodb 里，此时，我们就需要把 session 相关的操作修改为 async 模式。</p>
                        <p>理论上，我们可以依次修改每一个函数，让它们符合所依赖的函数的要求，但是这样就要求我们完全了解所有的模块并且有能力对其作出修改。这在多人协作开发时或者使用第三方模块时是完全不可能完成的。</p>
                        <p>因此，所有的通用模块，都需要同时提供 sync 和 async 接口，以均衡处理异步和性能之间的平衡。更多的普通开发者则会选择只提供 async 接口。从而引发性能灾难。</p>
                        <p>在 fibjs 中，你可以很轻松的解决类似的问题，避免显式异步无节制地传染:</p>
                        <pre><code class="lang-JavaScript"><div class="line-numbers"></div><span class="hljs-keyword">var</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);

                        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">session_get</span>(<span class="hljs-params">sid</span>) </span>{
                            <span class="hljs-keyword">return</span> sdata;
                        }

                        <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">async_session_get</span>(<span class="hljs-params">sid</span>) </span>{
                            <span class="hljs-keyword">return</span> sdata;
                        }

                        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callback_session_get</span>(<span class="hljs-params">sid, cb</span>) </span>{
                            cb(<span class="hljs-literal">null</span>, sdata);
                        }

                        data = session_get(sid);
                        data = util.sync(async_session_get)(sid);
                        data = util.sync(callback_session_get)(sid);
                        </code></pre>
                        <p>fibjs 可以使用 util.sync 将 callback 或者 async 函数转变为 sync 函数，并且直接调用。通过这种方式，我们可以很方便地整合不同编程范式的模块，并且以最小的开发成本将其转变为 sync 范式，有效地避免范式传染带来的灾难。</p>
                        <div name="开始体验" data-unique="开始体验"></div><h3 id="开始体验">开始体验</h3>
                        <p>准备好开始一场愉快的开发经历了吗？那么，从安装开始吧。</p>
                        <p>👉 【<a href="install.md.html">安装运行环境</a>】</p>
                         </div>
                        
                    <!--end of content-->
                    
                    <div class="col-md-2">
                        <div id="toc" class="tocify" style="top: 0px;">
                            <ul id="tocify-header0" class="tocify-header nav nav-list">
                                <li class="tocify-item active" data-unique="fibjs-是什么？" style="cursor: pointer;"><a>fibjs 是什么？</a>
                                </li>
                                <ul class="tocify-subheader nav nav-list" data-tag="3">
                                    <li class="tocify-item" data-unique="返璞归真，敏捷开发" style="cursor: pointer;"><a>返璞归真，敏捷开发</a></li>
                                    <li class="tocify-item" data-unique="拥抱高能" style="cursor: pointer;"><a>拥抱高能</a></li>
                                    <li class="tocify-item" data-unique="灵活选择范式而不被绑架" style="cursor: pointer;"><a>灵活选择范式而不被绑架</a></li>
                                    <li class="tocify-item" data-unique="开始体验" style="cursor: pointer;"><a>开始体验</a></li>
                                </ul>
                            </ul>
                        </div>
                    </div>
                    <!--end of right menu-->
                    
                </div>
            </div>
        </section>


		<div class="footer-container">
			<div class="footer-container">
			
		
			<footer class="short bg-secondary-1">
				<div class="container">
                    <div class="row">
						<div class="col-sm-10 copyright">
							<span>© Copright 2017 FibJS.org | <a href=support.html>支持我们</a></span>
							<!--ul>
								<li><a href="#">Terms Of Use</a></li>
								<li><a href="#">Privacy &amp; Security Statement</a></li>
								<li><a href="#">Sitemap</a></li>
							</ul-->
						</div>
						
						<div class="col-sm-2 text-right">
							<ul class="social-icons">
								<li>
									<a href="#">
										<i class="icon social_twitter"></i>
									</a>
								</li>
								
								<li>
									<a href="#">
										<i class="icon social_facebook"></i>
									</a>
								</li>
							</ul>	
						</div>
					</div><!--end of row-->
				</div><!--end of container-->
			</footer>
		</div>
				
		<script src="js/jquery.min.js"></script>
        <script src="js/jquery.plugin.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/jquery.flexslider-min.js"></script>
        <script src="js/smooth-scroll.min.js"></script>
        <script src="js/skrollr.min.js"></script>
        <script src="js/spectragram.min.js"></script>
        <script src="js/scrollReveal.min.js"></script>
        <script src="js/isotope.min.js"></script>
        <script src="js/twitterFetcher_v10_min.js"></script>
        <script src="js/lightbox.min.js"></script>
        <script src="js/jquery.countdown.min.js"></script>
        <script src="js/scripts.js"></script>
        
        
        <script type=text/javascript src=http://fibjs.org/js/common.js></script>
        <script type=text/javascript src=http://fibjs.org/js/index.js></script>
    </body>
</html>
				